﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BOHVote.BLL;
using BOHVote.Entity;
 

namespace BOH.Interview
{
    public partial class expertVote : System.Web.UI.Page
    {
        private BHUser user;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["User"] != null)
                {
                    user = (BHUser)Session["User"];
                    switch (user.RightType)
                    {
                        case "S":
                            LoadDrop(user.RightCode);
                            break;
                        case "C":
                        case "A":
                        default:
                            LoadDrop("");
                            break;
                    }
                }
            }
        }

        #region <<  drop down list  >>
        public void LoadDrop(string subjectID)
        {
            SubjectBindID(subjectID);
        }

        public void BindDropDownList(ListControl Controls, DataSet dst)
        {
            Controls.Items.Clear();
            ListItem list = new ListItem("请选择", "-1");
            Controls.Items.Add(list);
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                ListItem itemlist = new ListItem(dr["dict_Display"].ToString(), dr["dict_Value"].ToString());
                Controls.Items.Add(itemlist);
            }
        }

        #region <<  绑定申请学科组和专业  >>


        public void SubjectBindID(string SubjectID)
        {
            this.drpSubject.Items.Clear();
            //ListItem li1 = new ListItem("请选择学科组", "-1");
            //this.drpSubject.Items.Add(li1);
            if (String.IsNullOrEmpty(SubjectID))
            {
                DataSet dst = Subject.getAllASubject();

                foreach (DataRow dr in dst.Tables[0].Rows)
                {
                    if (dr["SubjectID"].ToString().Length > 0)
                    {
                        ListItem li = new ListItem(dr["SubjectName"].ToString(), dr["SubjectID"].ToString());
                        this.drpSubject.Items.Add(li);
                    }
                }
            }
            else
            {
                string SubjectName = Subject.getSubjectNameBySubjectID(SubjectID);
                ListItem li = new ListItem(SubjectName, SubjectID);
                this.drpSubject.Items.Add(li);
            }
        }

        public void SubjectBind(string level)
        {
            this.drpSubject.Items.Clear();
            DataSet dst;
            if (String.IsNullOrEmpty(level))
                dst = Subject.getAllASubject();
            else
                dst = Subject.getSubjectByLevel(level);
            //dst = Subject.
            //ListItem li1 = new ListItem("请选择学科组", "-1");
            //this.drpSubject.Items.Add(li1);
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                if (dr["SubjectID"].ToString().Length > 0)
                {
                    ListItem li = new ListItem(dr["SubjectName"].ToString(), dr["SubjectID"].ToString());
                    this.drpSubject.Items.Add(li);
                }
            }
        }

        #endregion

        #endregion

        #region <<  Data grid  >>
        //protected void AspNetPager1_PageChanged(object src, EventArgs e)
        //{
        //    GetVoteDataBind();
        //}

        private void GetVoteDataBind()
        {
            DataSet dst = InterviewClass.getResults("", "-1", this.drpSubject.SelectedValue, "-1", "-1", "-1", (string)Session["ApplyPeriod"], "G");
            DataSet dstS = InterviewClass.getGTotleNumber(this.drpSubject.SelectedValue, (string)Session["ApplyPeriod"]);
            if (dstS != null && dstS.Tables.Count > 0 && dstS.Tables[0].Rows.Count>0)
            {
                txtrequired.Text = dstS.Tables[0].Rows[0][0].ToString();
                txtactual.Text = dstS.Tables[0].Rows[0][1].ToString();
            }
            VoteBind(dst);
        }

        public void VoteBind(DataSet dst)
        {
            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                labNoRecord.Visible = false;
                ApplyViewVoteList.DataSource = dst;
                ApplyViewVoteList.DataBind();
                //AspNetPager1.RecordCount = dst.Tables[0].Rows.Count;
                //if (AspNetPager1.RecordCount != 0)
                //{
                //    labNoRecord.Visible = false;
                //    AspNetPager1.Visible = true;
                //    PagedDataSource pds = new PagedDataSource();
                //    pds.DataSource = dst.Tables[0].DefaultView;
                //    pds.AllowPaging = true;
                //    pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                //    pds.PageSize = AspNetPager1.PageSize;
                //    ApplyViewVoteList.DataSource = pds;
                //    ApplyViewVoteList.DataBind();
                //}
                //else
                //{
                //    ApplyViewVoteList.DataSource = null;
                //    ApplyViewVoteList.DataBind();
                //    AspNetPager1.Visible = false;
                //    labNoRecord.Visible = true;
                //}
            }
            else
            {
               // AspNetPager1.Visible = false;
                labNoRecord.Visible = true;
                ApplyViewVoteList.DataSource = null;
                ApplyViewVoteList.DataBind();
            }
        }
        #endregion

        protected void butResearch_Click(object sender, EventArgs e)
        {
            GetVoteDataBind();
        }

        protected void butsave_Click(object sender, EventArgs e)
        {
            
        }

        protected void ApplyViewVoteList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            ApplyViewVoteList.EditIndex = e.NewEditIndex;
            GetVoteDataBind();
        }

        protected void ApplyViewVoteList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            ApplyViewVoteList.EditIndex = -1;
            GetVoteDataBind();
        }

        protected void ApplyViewVoteList_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string TotalNumber = this.txtrequired.Text.Trim();
            string ActualNumber = this.txtactual.Text.Trim();
            if (!String.IsNullOrEmpty(TotalNumber) && !String.IsNullOrEmpty(ActualNumber))
            {
                GridViewRow gvr = ApplyViewVoteList.Rows[e.RowIndex];
                string ApplyID = ((HiddenField)gvr.FindControl("HiddenApplyID")).Value;
                string EvaluationType = "G";
                string AgreeNumber = ((TextBox)gvr.FindControl("txtAgree")).Text;
                string DenyNumber = ((TextBox)gvr.FindControl("txtDeny")).Text;
                string AbstrainNumber = ((TextBox)gvr.FindControl("txtAbstrain")).Text;

                string comment = ((TextBox)gvr.FindControl("txtRemark")).Text;
                string resultID = ((HiddenField)gvr.FindControl("hiddenResultID")).Value;
                if (String.IsNullOrEmpty(resultID))
                    InterviewClass.InsertInterviewResult(ApplyID, "", EvaluationType, TotalNumber, AgreeNumber, DenyNumber, AbstrainNumber, comment, ActualNumber);
                else
                    InterviewClass.UpdateInterviewResult(resultID, "", TotalNumber, AgreeNumber, DenyNumber, AbstrainNumber, comment, ActualNumber);
                ApplyViewVoteList_RowCancelingEdit(null, null);
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "confirm", "<script>alert('请输入应到人数和实到人数! ');</script>");
            }
        }

        protected void ApplyViewVoteList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "save")
            {
                string TotalNumber = this.txtrequired.Text.Trim();
                string ActualNumber = this.txtactual.Text.Trim();
                if (!String.IsNullOrEmpty(TotalNumber) && !String.IsNullOrEmpty(ActualNumber))
                {

                    GridViewRow gvr = ApplyViewVoteList.Rows[Convert.ToInt32(e.CommandArgument.ToString())];
                    string ApplyID = ((HiddenField)gvr.FindControl("HiddenApplyID")).Value;
                    string EvaluationType = "G";
                    string AgreeNumber = ((TextBox)gvr.FindControl("txtAgree")).Text;
                    string DenyNumber = ((TextBox)gvr.FindControl("txtDeny")).Text;
                    string AbstrainNumber = ((TextBox)gvr.FindControl("txtAbstrain")).Text;

                    string comment = ((TextBox)gvr.FindControl("txtRemark")).Text;
                    string resultID = ((HiddenField)gvr.FindControl("hiddenResultID")).Value;
                    if (string.IsNullOrEmpty(AbstrainNumber))
                    {
                        AbstrainNumber = (Convert.ToInt32(TotalNumber) - (Convert.ToInt32(AgreeNumber) + Convert.ToInt32(DenyNumber))).ToString();
                    }
                    if (String.IsNullOrEmpty(resultID))
                        InterviewClass.InsertInterviewResult(ApplyID, "", EvaluationType, TotalNumber, AgreeNumber, DenyNumber, AbstrainNumber, comment, ActualNumber);
                    else
                        InterviewClass.UpdateInterviewResult(resultID, "", TotalNumber, AgreeNumber, DenyNumber, AbstrainNumber, comment, ActualNumber);
                    
                    GetVoteDataBind();
                }
                else
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "confirm", "<script>alert('请输入应到人数和实到人数! ');</script>");
                }
            }
        }

        protected void SaveAll_Click(object sender, EventArgs e)
        {
            string TotalNumber = this.txtrequired.Text.Trim();
            string ActualNumber = this.txtactual.Text.Trim();
            if (!String.IsNullOrEmpty(TotalNumber) && !String.IsNullOrEmpty(ActualNumber))
            {
                foreach (GridViewRow gvr in ApplyViewVoteList.Rows)
                {
                    //GridViewRow gvr = ApplyViewVoteList.Rows[Convert.ToInt32(e.CommandArgument.ToString())];
                    string ApplyID = ((HiddenField)gvr.FindControl("HiddenApplyID")).Value;
                    string EvaluationType = "G";
                    string AgreeNumber = ((TextBox)gvr.FindControl("txtAgree")).Text;
                    string DenyNumber = ((TextBox)gvr.FindControl("txtDeny")).Text;
                    string AbstrainNumber = ((TextBox)gvr.FindControl("txtAbstrain")).Text;

                    string comment = ((TextBox)gvr.FindControl("txtRemark")).Text;
                    string resultID = ((HiddenField)gvr.FindControl("hiddenResultID")).Value;
                    if(!string.IsNullOrEmpty(AgreeNumber)&&!string.IsNullOrEmpty(DenyNumber))
                    {
                        if (string.IsNullOrEmpty(AbstrainNumber))
                        {
                            AbstrainNumber = (Convert.ToInt32(TotalNumber) - (Convert.ToInt32(AgreeNumber) + Convert.ToInt32(DenyNumber))).ToString();
                        }

                        if (String.IsNullOrEmpty(resultID))
                            InterviewClass.InsertInterviewResult(ApplyID, "", EvaluationType, TotalNumber, AgreeNumber, DenyNumber, AbstrainNumber, comment, ActualNumber);
                        else
                            InterviewClass.UpdateInterviewResult(resultID, "", TotalNumber, AgreeNumber, DenyNumber, AbstrainNumber, comment, ActualNumber);
                        
                    }
                }
                GetVoteDataBind();
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "confirm", "<script>alert('请输入应到人数和实到人数! ');</script>");
            }
        }
    }
}
